﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AdminManageUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            if (Session["username"].Equals("admin@mail.com"))
            {
                loadData();
            }
            else
            {
                Response.Redirect("~/login.aspx");
            }
        }
        else
        {
            Response.Redirect("~/login.aspx");
        }
    }
    protected void btncreateuser_Click(object sender, EventArgs e)
    {
        string chkUser = "Select * FROM [User_] where Email='" + TextBoxEmail.Text + "'";
        DatabaseProcess dp = new DatabaseProcess();
        DataTable dt = dp.ConnectDataBaseReturnDT(chkUser);
        if (dt.Rows.Count > 0)
        {
            check_mail.Text = "Duplicate Email!!! choose another email address, please";
        }
        else
        {
            string RegisterQuery = "INSERT INTO [User_] (Email,Password_,Name,Country,role) VALUES('" + TextBoxEmail.Text + "','" + TextBoxPassword.Text + "','" + TextBoxName.Text + "','" + TextBoxCountry.Text + "',1)";
            dp.ConnectDataBaseToInsert(RegisterQuery);
            loadData();
        }
    }

    protected void loadData() {
        DatabaseProcess dp = new DatabaseProcess();
        String query = "select * from User_ where role < " + Int32.Parse(Session["role"].ToString());
        DataTable dt = dp.ConnectDataBaseReturnDT(query);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}